#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "termInput.h"

int addChar(termInput term, char c) {
	int j;

	if(++(term->size) == term->buffer_size) {
		term->buffer_size += term->buffer_increase_rate;
		term->buffer = (char *)realloc(term->buffer,term->buffer_size*sizeof(char));
	}
	for(j=term->size-1;j >= term->cursor_pos;j--) {
		term->buffer[j+1] = term->buffer[j];
	}
	term->buffer[++j] = c;

	return 1;
}

int removeChar(termInput term) {
	int j;

	if(term->cursor_pos == 0 || term->cursor_pos > size) {
		return 0;
	}

	for(j=term->cursor_pos-1;j < term->size;j++) {
		term->buffer[j] = term->buffer[j+1];		
	}

	(term->size)--;

	return 1;		

}

termInput createTermInput(int buffer_size, int buffer_increase_rate) {
	termInput t = (termInput)malloc(sizeof(struct _TERMINAL_INPUT));
	
	if(t == NULL)
		return NULL;
	
	t->buffer_size = buffer_size;
	t->buffer_increase_rate = buffer_increase_rate;
	t->buffer = (char *)malloc(buffer_size*sizeof(char));

	if(t->buffer == NULL)
		return NULL;

	t->buffer[0] = '\0';
	t->size = 0;
	t->cursor_pos = 0;

	return t;
}

